#include "Common.h"
#include "VertexIndexData.h"
#include "Render.h"
#include <d3d9.h>

//
// VertexData
//
VertexData::VertexData(const StreamInfo& pVB, const StreamInfo& npVB)
	: mPositionStreamInfo(pVB)
	, mNoPositionStreamInfo(npVB)
{
}

VertexData::~VertexData()
{
	SAFE_RELEASE(mPositionStreamInfo.pStreamData);
	SAFE_RELEASE(mNoPositionStreamInfo.pStreamData);
}

void VertexData::Set(bool positionOnly) const
{
	LPDIRECT3DDEVICE9 device = Render::Instance().getDevice();

	VERIFY_D3D(device->SetStreamSourceFreq(mPositionStreamInfo.StreamNumber, 1));
	VERIFY_D3D(device->SetStreamSource(
		mPositionStreamInfo.StreamNumber, 
		mPositionStreamInfo.pStreamData, 
		mPositionStreamInfo.OffsetInBytes, 
		mPositionStreamInfo.Stride));

	if (positionOnly)
		return;
	
	VERIFY_D3D(device->SetStreamSourceFreq(mNoPositionStreamInfo.StreamNumber, 1));
	VERIFY_D3D(device->SetStreamSource(
		mNoPositionStreamInfo.StreamNumber, 
		mNoPositionStreamInfo.pStreamData, 
		mNoPositionStreamInfo.OffsetInBytes, 
		mNoPositionStreamInfo.Stride));
}

//
// IndexData
//
IndexData::IndexData()
	: mIndexBuffer(NULL)
{
}

IndexData::IndexData(IDirect3DIndexBuffer9* pIB, const PrimitiveInfo& primitiveInfo)
	: mIndexBuffer(pIB)
	, mPrimitiveInfo(primitiveInfo)
{
}

IndexData::~IndexData()
{
	SAFE_RELEASE(mIndexBuffer);
}